<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>

        <el-form-item label="房屋类型:">
          <el-select v-model="listPram.houseType" placeholder="全部" style="width: 150px">
            <el-option
                v-for="item in houseTypeOptions"
                :key="item.value"
                :label="item.houseTypeName"
                :value="item.houseTypeName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属房屋:">
          <el-input v-model="listPram.fwkeyword" placeholder="请输入租赁证编号、地址关键词" clearable style="width: 250px"/>
        </el-form-item>
        <el-form-item label="承租人:">
          <el-input v-model="listPram.yhkeyword" placeholder="姓名、身份证号、手机号关键词" clearable style="width: 250px"/>
        </el-form-item>
        <el-form-item label="合同编号关键词:">
          <el-input v-model="listPram.contractNumber" placeholder="请输入租赁合同编号关键词" clearable style="width: 250px"/>
        </el-form-item>

        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button
              icon="el-icon-upload2"
              @click="handleExport('multi')"
              type="primary" size="mini" v-has-permi="['admin:house:information:export']">
            批量导出
          </el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-upload2" size="mini" type="primary" v-has-permi="['admin:house:information:import']">批量导入
          </el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <router-link :to=" { path:'/leaseInformation/effective/edit/' } ">
            <el-button icon="el-icon-edit" size="mini" type="primary"
                       v-has-permi="['admin:leaseInformation:effective:save']">新增
            </el-button>
          </router-link>
        </el-form-item>

      </el-form>
      <el-table :data="listData.items" size="small"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border
                @selection-change="handleSelectionChange">
        <el-table-column type="expand" align="center" width="40">
          <template slot-scope="props">
            <el-form label-position="left" label-width="100px" inline class="demo-table-expand" style="margin-left: 50px;">
              <el-row>
                <el-form-item label="所属房屋：">
                  <span>{{ props.row.houseType }}</span>
                </el-form-item>
                <el-form-item label="地段：">
                  <span>{{ props.row.houseAreaType }}</span>
                </el-form-item>
                <el-form-item label="楼层：">
                  <span>{{ props.row.floorLevel }}</span>
                </el-form-item>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column align="center"
                         type="selection"
                         width="50">
        </el-table-column>
        <el-table-column align="center" label="ID" prop="id" width="60"></el-table-column>
        <el-table-column align="center" label="租赁合同编号" prop="contractNumber" width="100"/>
        <el-table-column align="center" label="关联房屋" prop="houseType" min-width="300">
          <template slot-scope="scope">
                <span @click="handleHouse(scope.row.idValue)" class="clickable-link">
                  {{ scope.row.leaseCertificateNumber }}-{{ scope.row.areaName }}{{ scope.row.streetName }}-{{ scope.row.mphValue }}
                </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="关联承租人" prop="currentTenantName" width="100">
          <template slot-scope="scope">
              <span :style="{
                border: scope.row.isArrearage === 1 ? '2px solid #ff4949' : 'none',
                padding: '2px',
                display: 'inline-block'
              }">
                {{ scope.row.currentTenantName }}
              </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="合同租赁周期" prop="timePercentage" width="300">
          <template slot-scope="scope">
            <span>{{ scope.row.paymentStart }}至{{ scope.row.paymentEnd }}</span>
          </template>
        </el-table-column>
        <el-table-column label="收费标准" header-align="center">
          <el-table-column align="center" label="应交" prop="unitPricePerSquare" width="100"/>
          <el-table-column align="center" label="实收(调差后)" prop="calculatedPrice" width="100"/>
        </el-table-column>
        <el-table-column align="center" label="建筑面积/使用面积" prop="streetName" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.buildingArea }}㎡/{{ scope.row.usableArea }}㎡</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="月租金" prop="totalPrice" width="100">
          <template slot-scope="scope" >
              <span @click="showFormula(scope.row)" class="clickable-link">
                ￥{{ scope.row.totalPrice || 0}}
              </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="租赁合同状态" prop="status" width="120">
          <template slot-scope="scope">
              <span :style="getStatusStyle(scope.row.status)">
                {{ getStatusText(scope.row.status) }}
              </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="关联附件" prop="contractAddress" width="100">
          <template slot-scope="scope">
            <span @click="handlePreview(scope.row.contractAddress)" class="clickable-link">查看</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <router-link :to=" { path:'/leaseInformation/effective/edit/' + scope.row.id + '/1'}">
              <el-button icon="el-icon-edit" size="mini" type="primary" style="margin-left: 10px"
                         v-has-permi="['admin:leaseInformation:effective:detail']">详情
              </el-button>
            </router-link>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handlerDelete(scope.row.id)" style="margin-left: 10px"
                       v-has-permi="['admin:leaseInformation:effective:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[10,20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog title="月租金计算公式" :visible.sync="dialogVisible2" width="50%">
      <div>
        <p><strong>计算公式：</strong>应交月租金 = 使用面积 x 调差后收费标准</p>
        <div v-if="currentRow2">
          <p style="margin-top: 10px;margin-left: 145px"  > =
            {{currentRow2.usableArea || 0}} x {{currentRow2.calculatedPrice || 0}} = ￥{{currentRow2.totalPrice || 0}}
          </p>
        </div>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import {deleteRow, getAdminGetFwlx} from "@/api/house";
import {list} from "@/api/leaseInformation";
import {checkPermi} from "@/utils/permission";
import {exportMultiColumn} from "@/api/export";

export default {
  data() {
    return {
      listData: {
        items: [], total: 0
      },
      listPram: {
        page: 1,
        limit: 10,
        contractNumber: '',
        status: 3,
        fwkeyword: '',
        houseType: '',
        yhkeyword: '',
      },
      editDialog: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editDate: {},
      },
      tableLoading: false,
      houseTypeOptions: [],
      multipleSelection: [],

      dialogVisible2: false,
      currentRow2: null,

    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
    this.getHouseType()
  },
  methods: {
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
        this.tableLoading = false;
      })

    },
    handleSearch() {
      this.listPram.page = 1;
      this.getList();
    },
    getHouseType() {
      // 获取房屋类型
      getAdminGetFwlx().then((res) => {
        this.houseTypeOptions = res
      })
    },
    // 显示弹窗
    showFormula (row) {
      this.currentRow2 = row;
      this.dialogVisible2 = true;
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    handleExport(){
      exportMultiColumn().then(() => {
      }).finally(() => {
      })
    },
    hideEditDialog() {
      this.editDialog.visible = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    resetQuery() {
      this.listPram.contractNumber = '';
      this.listPram.fwkeyword = '';
      this.listPram.houseType = '';
      this.listPram.yhkeyword = '';
      this.listPram.page = 1;
      this.getList()
    },
    handleHouse(currentHouse) {
      this.$router.push({
        path: '/house/information',
        query: {
          currentHouse: currentHouse // 携带当前承租人名称作为查询参数
        }
      });
    },
    handlePreview(url) {
      window.open(url, '_blank');
    },
    getStatusStyle(status) {
      switch (status) {
        case 1: return { color: '#e6a23c' } // 待审核 - yellow
        case 2: return { color: '#f56c6c' } // 生效中 - red
        case 3: return { color: '#409eff' } // 已终止 - blue
        case 4: return { color: '#f56c6c' } // 争议处理中 - red
        default: return {}
      }
    },
    getStatusText(status) {
      const statusMap = {
        1: '待审核',
        2: '生效中',
        3: '已终止',
        4: '争议处理中'
      }
      return statusMap[status] || '未知状态'
    },

  }
}
</script>

<style scoped lang="scss">
::v-deep .el-checkbox__inner {
  border-color: #1879fe;
}
.clickable-link {
  color: #1890ff;
  cursor: pointer;
  text-decoration: none;
}

.clickable-link:hover {
  text-decoration: underline;
  color: #1890ff;
}
</style>
